package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.ManufactureDesignProcedureDetailsModuleDto;
import com.guigu.erp.dto.ManufactureDesignProcedureDto;
import com.guigu.erp.dto.Vo;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ModuleFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:ManufactureDesignProcedureController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/12 0:24
 * @Author:chenchong
 */
@Controller
@RequestMapping("/ManufactureDesignProcedure")
public class ManufactureDesignProcedureController {

    @Autowired
    private NumberUtils numberUtils;
    @Autowired
    private DesignFileService designFileService;
    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;
    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;
    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    //处理制定物料设计单的确认按钮请求
    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody ManufactureDesignProcedureDto manufactureDesignProcedureDto) {
        //生成工序设计单编号
        String typeNumber = numberUtils.getTypeNumber("201");

        manufactureDesignProcedureDto.setDesignId(typeNumber);

        //产品工序的总成本
        List<ManufactureDesignProcedureDetails> details = manufactureDesignProcedureDto.getDetails();
        BigDecimal now = new BigDecimal(0);

        for (int i = 0; i < details.size(); i++) {

            BigDecimal w = details.get(i).getSubtotal();
            now = now.add(w);
        }

        //赋值
        manufactureDesignProcedureDto.setCostPriceSum(now);


        //设置审核标志 S001-0 等待审核
        manufactureDesignProcedureDto.setCheckTag("S001-0");

        //设置变更标志 B002-0 未变更
        manufactureDesignProcedureDto.setChangeTag("B002-0");

        //工序物料设计标志 G002-0: 未设计
        manufactureDesignProcedureDto.setDesignModuleTag("G002-0");

        //工序物料变更标志 G003-0 未变更
        manufactureDesignProcedureDto.setDesignModuleChangeTag("G003-0");

        //设置当前物料设计单的分类编号
        //获取产品档案编号
        String productId = manufactureDesignProcedureDto.getProductId();

        //根据产品档案编号查询分类级别
        QueryWrapper qw = new QueryWrapper();
        qw.eq("product_id", productId);
        DesignFile one = designFileService.getOne(qw);
        if (one != null) {
            manufactureDesignProcedureDto.setFirstKindId(one.getFirstKindId());
            manufactureDesignProcedureDto.setFirstKindName(one.getFirstKindName());
            manufactureDesignProcedureDto.setSecondKindId(one.getSecondKindId());
            manufactureDesignProcedureDto.setSecondKindName(one.getFirstKindName());
            manufactureDesignProcedureDto.setThirdKindId(one.getThirdKindId());
            manufactureDesignProcedureDto.setThirdKindName(one.getThirdKindName());
        }

        manufactureDesignProcedureService.makeDesignProcedure(manufactureDesignProcedureDto);
        return "ok";
    }

    /**
     * 产品生产工序审核
     *
     * @return
     */
    @RequestMapping("/audit")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> audit() {
        PageUtils<ManufactureDesignProcedure> page = manufactureDesignProcedureService.page();
        System.out.println(page);
        return page;
    }

    /**
     * 产品生产工序审核
     *
     * @return
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public ManufactureDesignProcedure getOne(int id) {
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("id", id);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(qw);
        return one;
    }

    @RequestMapping("/getOne2")
    @ResponseBody
    public ManufactureDesignProcedure getOne(String productId) {
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_ID", productId);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(qw);
        return one;
    }
    /**
     * 产品生产工序审核
     *
     * @return
     */
    @RequestMapping("/showDetails")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedureDetails> showDetails(int id) {
        PageUtils<ManufactureDesignProcedureDetails> p = manufactureDesignProcedureService.showDetails(id);
        return p;
    }
    /**
     * 工序明细
     *
     * @return
     */
    @RequestMapping("/getoneDetails")
    @ResponseBody
    public ManufactureDesignProcedureDetails getoneDetails(int id) {

        ManufactureDesignProcedureDetails byId = manufactureDesignProcedureDetailsService.getById(id);
        return byId;
    }
    /**
     * 产品生产工序审核
     *
     * @return
     */

    @ResponseBody
    @RequestMapping("/update")
    public boolean update(@RequestBody ManufactureDesignProcedure designModule) {

        boolean b = manufactureDesignProcedureService.updateById(designModule);
        return b;
    }

    /**
     * 产品生产工序审核
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/procedureShow")
    public PageUtils<ManufactureDesignProcedure> procedureShow(ModuleFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureDesignProcedure> designModulePageUtils = manufactureDesignProcedureService.procedureShow(vo, pageNo, pageSize);

        return designModulePageUtils;
    }

    @ResponseBody
    @RequestMapping("/procedureShow10")
    public PageUtils<ManufactureDesignProcedure> procedureShow10() {
        PageUtils<ManufactureDesignProcedure> designModulePageUtils = manufactureDesignProcedureService.procedureShow10();
        return designModulePageUtils;
    }
    @ResponseBody
    @RequestMapping("/procedureShow11")
    public PageUtils<ManufactureDesignProcedure> procedureShow11(ModuleFileVo vo,int pageNo, int pageSize) {
        PageUtils<ManufactureDesignProcedure> designModulePageUtils = manufactureDesignProcedureService.procedureShow11(vo,pageNo,pageSize);
        return designModulePageUtils;
    }
    @ResponseBody
    @RequestMapping("/addProcedureModule")
    public String addProcedureModule(@RequestBody ManufactureDesignProcedureDetailsModuleDto dto) {
        manufactureDesignProcedureModuleService.addPage(dto);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/updateProcedureModule")
    public Boolean updateProcedureModule(@RequestBody ManufactureDesignProcedureDetailsModuleDto dto) {
        //产品编号
        // "productId":
        //工序编号
        // "procedureId"
        Boolean b = manufactureDesignProcedureModuleService.updateProcedureModule(dto);
        return b;

    }

    @ResponseBody
    @RequestMapping("/ListGW")
    public ManufactureDesignProcedureDetails ListGWM(int id, String procedureId) {
        QueryWrapper<ManufactureDesignProcedureDetails> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", id);
        qw.eq("PROCEDURE_ID", procedureId);
        ManufactureDesignProcedureDetails list = manufactureDesignProcedureDetailsService.getOne(qw);
        return list;
    }

    @ResponseBody
    @RequestMapping("/ListGWM")
    public List<ManufactureDesignProcedureModule> ListGWM(int id) {
        QueryWrapper<ManufactureDesignProcedureModule> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", id);
        List<ManufactureDesignProcedureModule> list = manufactureDesignProcedureModuleService.list(qw);
        return list;
    }

    @ResponseBody
    @RequestMapping("/getListgxmx")
    public List<ManufactureDesignProcedureDetails> getListgxmx(String productId) {
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_ID", productId);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(qw);
        QueryWrapper<ManufactureDesignProcedureDetails> qw1 = new QueryWrapper<>();
        qw1.eq("PARENT_ID", one.getId());
        List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(qw1);
        return list;
    }

    @ResponseBody
    @RequestMapping("/getListgxmx2")
    public boolean getListgxmx2(String productId) {
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_ID", productId);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(qw);
        QueryWrapper<ManufactureDesignProcedureDetails> qw1 = new QueryWrapper<>();
        qw1.eq("PARENT_ID", one.getId());
        List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(qw1);
        BigDecimal now = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            BigDecimal moduleSubtotal = list.get(i).getModuleSubtotal();
            now = now.add(moduleSubtotal);
        }

        ManufactureDesignProcedure m = new ManufactureDesignProcedure();
        m.setId(one.getId());
        m.setModuleCostPriceSum(now);
        m.setDesignModuleTag("G002-1");
        boolean update = manufactureDesignProcedureService.updateById(m);
        return update;
    }

    @ResponseBody
    @RequestMapping("/designModuleShow")
    public PageUtils<ManufactureDesignProcedure> designModuleShow() {
        PageUtils<ManufactureDesignProcedure> list = manufactureDesignProcedureService.designModuleShow();
        return list;
    }
    @ResponseBody
    @RequestMapping("/shenhe")
    public String shenhe(@RequestBody Vo vo) {

        manufactureDesignProcedureModuleService.shenhe(vo);
        String checkTag = vo.getCheckTag();
        if(checkTag.equals("G002-0")){
            return "ok1";
        }
        return "ok";
    }
}
